package com.android.datn.daos;

import java.util.ArrayList;

import com.android.datn.helper.DBBinding;
import com.android.datn.objects.Track;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TrackDAO extends BaseDAO {
	private static TrackDAO instance;
	private Context context;

	public TrackDAO(Context context) {
		super(context);
		this.context = context;
	}

	public static void initInstance(Context context) {
		if (instance == null) {
			// Create the instance
			instance = new TrackDAO(context);
		}
	}

	public static TrackDAO getInstance() {
		// Return the instance
		return instance;
	}

	public ArrayList<Track> getAllTrack(int type) {
		boolean fav = false;
		
		openDataBase();
		String query = null;
		ArrayList<Track> tracks = new ArrayList<Track>();
		if (type == DBBinding.DB_FAVORITE) {
			fav = true;
			query = "SELECT * FROM favorite_tracks;";
		} else if (type == DBBinding.DB_VOTE) {
			query = "SELECT * FROM vote_tracks;";
		}
		ArrayList<ArrayList<String>> stringList = selectRecordsFromDBList(
				query, null);
		close();
		for (int i = 0; i < stringList.size(); i++) {
			ArrayList<String> list = stringList.get(i);
			Track track = new Track(Integer.parseInt(list.get(0)));
			try {
				track.setTitle(list.get(1));
				track.setArtist(list.get(2));
				track.setLink(list.get(3));
				track.setType(Integer.parseInt(list.get(4)));
				track.setCreated(list.get(5));
				track.setVote(list.get(6));
				track.setListen(list.get(7));
				track.setTrackNumber(Integer.parseInt(list.get(8)));
				track.setDuration(Integer.parseInt(list.get(9)));
				track.setIdCategory(Integer.parseInt(list.get(10)));
				track.setListening(false);
				track.setPercentCurrent(0);
				track.setPrepared(false);
				track.setFavorite(fav);
			} catch (Exception e) {
				Log.i(context.getClass().getSimpleName(), e.getMessage());
			}
			tracks.add(track);
		}
		return tracks;
	}

	public ArrayList<Track> getAllTrackQuery(Track tr, int type) {
		boolean fav = false;
		
		openDataBase();
		String query = null;
		ArrayList<Track> tracks = new ArrayList<Track>();
		if (type == DBBinding.DB_FAVORITE) {
			fav = true;
			query = "SELECT * FROM  favorite_tracks where title = '"
					+ tr.getTitle().trim() + "' and " + "artist = '"
					+ tr.getArtist().trim() + "' and " + "id_category = '"
					+ tr.getIdCategory() + "'";
		} else if (type == DBBinding.DB_VOTE) {
			query = "SELECT * FROM  vote_tracks where title = '"
					+ tr.getTitle().trim() + "' and " + "artist = '"
					+ tr.getArtist().trim() + "' and " + "id_category = '"
					+ tr.getIdCategory() + "'";
		}
		ArrayList<ArrayList<String>> stringList = selectRecordsFromDBList(
				query, null);
		close();
		for (int i = 0; i < stringList.size(); i++) {
			ArrayList<String> list = stringList.get(i);
			Track track = new Track(Integer.parseInt(list.get(0)));
			try {
				track.setTitle(list.get(1).trim());
				track.setArtist(list.get(2).trim());
				track.setLink(list.get(3));
				track.setType(Integer.parseInt(list.get(4)));
				track.setCreated(list.get(5));
				track.setVote(list.get(6));
				track.setListen(list.get(7));
				track.setTrackNumber(Integer.parseInt(list.get(8)));
				track.setDuration(Integer.parseInt(list.get(9)));
				track.setIdCategory(Integer.parseInt(list.get(10)));
				track.setListening(false);
				track.setPercentCurrent(0);
				track.setPrepared(false);
				track.setFavorite(fav);
			} catch (Exception e) {
				Log.i(context.getClass().getSimpleName(), e.getMessage());
			}
			tracks.add(track);
		}
		
		Log.i("DAOOOOOOOOOOOOOOOOOOOOOOO", fav+"");
		
		return tracks;
	}

	public int addTrack(Track track, int type) {
		int newID = 0;
		ContentValues values = new ContentValues();
		values.put("title", track.getTitle().trim());
		values.put("artist", track.getArtist().trim());
		values.put("link", track.getLink());
		values.put("type", track.getType());
		values.put("created", track.getCreated());
		values.put("vote", track.getVote());
		//values.put("listen", track.getListen());
		values.put("track_number", track.getTrackNumber());
		values.put("duration", track.getDuration());
		values.put("id_category", track.getIdCategory());
		openDataBase();
		if (type == DBBinding.DB_FAVORITE) {
			values.put("listen", 0);
			newID = (int) insertRecordsInDB("favorite_tracks", null, values);
		} else if (type == DBBinding.DB_VOTE) {
			
		}
		newID = (int) insertRecordsInDB("vote_tracks", null, values);
		close();
		return newID;
	}

	public int getSizeByTitleAndArtist(Track track, int type) {
		return getAllTrackQuery(track, type).size();
	}

	public void deleteFavoriteByID(int id) {
		openDataBase();
		deleteRecordInDB("favorite_tracks", "id=?",
				new String[] { String.valueOf(id) });
		close();
	}

	public void deleteFavorite(Track track, int type) {
		openDataBase();
		String table_name = null;
		if (type == DBBinding.DB_FAVORITE) {
			table_name = "favorite_tracks";
		} else if (type == DBBinding.DB_VOTE) {
			table_name = "vote_tracks";
		}
		String where = "title = '" + track.getTitle().trim() + "'"
				+ " AND artist = '" + track.getArtist().trim() + "'"
				+ " AND id_category = '" + track.getIdCategory() + "'";
		deleteRecordInDB(table_name, where, null);
		close();
	}

	public void deleteTable(int type) {
		if (type == DBBinding.DB_VOTE) {
			// deleteAllRows(DBBinding.VOTE_TABLE);
			SQLiteDatabase db = this.getWritableDatabase();
			db.delete(DBBinding.VOTE_TABLE, null, null);
		}
	}

	public void updateTable(Track track, int type) {
		if (type == DBBinding.DB_VOTE) {
			// deleteAllRows(DBBinding.VOTE_TABLE);
			SQLiteDatabase sqdb = this.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put("vote", track.getVote());
			int id = sqdb.update(
					"vote_tracks",
					values,
					"id_category='" + track.getIdCategory() + "' and title='"
							+ track.getTitle() + "' " + "and artist='"
							+ track.getArtist() + "'", null);
		}
	}
}
